﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace work6._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeUI();
        }

        private void InitializeUI()
        {
            // 初始化树形视图和列表视图
            treeView1.AfterSelect += TreeView1_AfterSelect;
            listView1.View = View.Details;
            listView1.Columns.Add("名称", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("类型", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("大小", 100, HorizontalAlignment.Left);

            // 初始化树形视图
            LoadDrives();
        }

        private void LoadDrives()
        {
            treeView1.Nodes.Clear();
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    TreeNode driveNode = new TreeNode(drive.Name);
                    driveNode.Tag = drive.RootDirectory;
                    treeView1.Nodes.Add(driveNode);
                    LoadDirectories(driveNode, drive.RootDirectory);
                }
            }
        }

        private void LoadDirectories(TreeNode parentNode, DirectoryInfo dir)
        {
            try
            {
                parentNode.Nodes.Clear();
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    TreeNode node = new TreeNode(subDir.Name);
                    node.Tag = subDir;
                    parentNode.Nodes.Add(node);
                    LoadDirectories(node, subDir);
                }
                foreach (FileInfo file in dir.GetFiles())
                {
                    TreeNode node = new TreeNode(file.Name);
                    node.Tag = file;
                    parentNode.Nodes.Add(node);
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 忽略无权限访问的目录
            }
        }

        private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            object tag = e.Node.Tag;
            if (tag is DirectoryInfo dir)
            {
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    ListViewItem item = new ListViewItem(new[] { subDir.Name, "文件夹", "" });
                    listView1.Items.Add(item);
                }
                foreach (FileInfo file in dir.GetFiles())
                {
                    ListViewItem item = new ListViewItem(new[] { file.Name, file.Extension, file.Length.ToString() });
                    listView1.Items.Add(item);
                }
            }
        }

        private void RefreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDrives();
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ListView1_ItemActivate(object sender, EventArgs e)
        {
            ListViewItem selectedItem = listView1.SelectedItems[0];
            object tag = selectedItem.Tag;
            if (tag is FileInfo file)
            {
                if (file.Extension.ToLower() == ".xml")
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreWhitespace = true;
                    using (XmlReader reader = XmlReader.Create(file.FullName, settings))
                    {
                        while (reader.Read())
                        {
                            switch (reader.NodeType)
                            {
                                case XmlNodeType.Element:
                                    Console.WriteLine($"元素: {reader.Name}");
                                    break;
                                case XmlNodeType.Attribute:
                                    Console.WriteLine($"属性: {reader.Name} = {reader.Value}");
                                    break;
                                case XmlNodeType.Text:
                                    Console.WriteLine($"文本: {reader.Value}");
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }

}
